package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.Signin;
import cn.wolfcode.crm.mapper.DepartmentMapper;
import cn.wolfcode.crm.mapper.SigninMapper;
import cn.wolfcode.crm.query.SigninQueryObject;
import cn.wolfcode.crm.service.ISigninService;
import cn.wolfcode.crm.util.LogicException;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class SigninServiceImpl implements ISigninService {
    @Autowired
    private SigninMapper mapper;

    @Autowired
    private DepartmentMapper departmentMapper;

    public void delete(Long id) {
        Employee currentUser = (Employee) SecurityUtils.getSubject().getPrincipal();
        if (!currentUser.isAdmin()){
            throw new LogicException("您不是超级管理员,没有删除权限");
        }
        mapper.deleteByPrimaryKey(id);
    }

    public void save(Signin record){
        Employee currentUser = (Employee) SecurityUtils.getSubject().getPrincipal();
        int count = mapper.selectCountByEmpIdAndStatusIsIn(currentUser.getId());
        //如果数据条数>=1
        if (count>=1){
            List<Signin> list = mapper.selectListByEmpIdAndStatusIsIn(currentUser.getId());
            for (Signin signin : list) {
                if (signin.getIndate().getDate()==new Date().getDate()){
                    throw new LogicException("您已经签到了,无法再次签到");
                }
            }
        }else if (count ==0){
            List<Signin> list2 = mapper.selectListByEmpIdAndStatusIsOut(currentUser.getId());
            for (Signin signin : list2) {
                if (signin.getIndate().getDate() == new Date().getDate()) {
                    throw new LogicException("您已经签到了,无法再次签到");
                }
            }
        }
         record.setEmployee(currentUser);
        record.setDept(departmentMapper.selectDeptByEmpId(currentUser.getId()));
        record.setIndate(new Date());
        record.setStatus(Signin.STATUS_SIGNED_IN);
        mapper.insert(record);
    }

    public Signin get(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    public List<Signin> list() {
        return mapper.selectAll();
    }

    public void update(Signin record) {
        Employee currentUser = (Employee) SecurityUtils.getSubject().getPrincipal();
        int count = mapper.selectCountByEmpIdAndStatusIsOut(currentUser.getId());
        if (count >= 1) {
            List<Signin> list = mapper.selectListByEmpIdAndStatusIsOut(currentUser.getId());
            for (Signin signin : list) {
                if (signin.getOutdate().getDate() == new Date().getDate()) {
                    throw new LogicException("您已经签退了,无法再次签退");
                }
            }
        }else{
            List<Signin> list = mapper.selectListByEmpIdAndStatusIsIn(currentUser.getId());
            for (Signin signin : list) {
                if (signin.getIndate().getDate() == new Date().getDate()) {
                    record.setOutdate(new Date());
                    record.setStatus(Signin.STATUS_SIGNED_OUT);
                    record.setId(signin.getId());
                    mapper.updateByPrimaryKey(record);
                }
            }
        }
    }

    //分页
    public PageResult query(SigninQueryObject qo){

        int count = mapper.queryForCount(qo);
        List<Signin> employeeList = mapper.queryForList(qo);
        if (count==0){
            return new PageResult();
        }
        return new PageResult(count,employeeList);
    }
}
